<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class Options {

    public $id;
    public $userId;
    public $name;
    public $value;

    public function __construct($name) {
        $this->name = $name;
        $this->userId = User::getCurrentUserId();
        $db = Database::getInstance();
        $query = "SELECT * FROM user_options WHERE name = '$name' AND userid = $this->userId";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow == 1) {
                $row = $result->dataTable->rows[0];
                $this->id = $row['id'];
                $this->value = $row['value'];
            }
            return $this;
        }
    }

    public static function getOption($name, $type = "int") {
        $instance = new self($name);
        switch ($type) {
            case "int":
                return 0 + $instance->value;
            case "array":
                return json_decode($instance->value);
        }
    }

    public static function getAllUserOptions($name, $type = "int", $value = '') {
        $db = Database::getInstance();
        $query = "SELECT * FROM user_options WHERE name = '$name'";
        if ($value !== '') {
            $whereParam = json_decode($value);
            foreach ($whereParam as $key => $value)
                if ($value[0] !== '!') {
                    $query .= " AND $key='$value'";
                } else {
                    $value = substr($value, 1);
                    $query .= " AND $key != '$value'";
                }
        }
        $result = $db->query($query);
        $options = array();
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow == 1) {
                $row = $result->dataTable->rows[0];
                $instance = new self();
                $instance->name = $row['name'];
                $instance->userId = 0 + $row['userid'];
                $instance->id = 0 + $row['id'];
                switch ($type) {
                    case "int":
                        $instance->value = 0 + $row['value'];
                        break;
                    case "array":
                        $instance->value = json_decode($row['value']);
                        break;
                }
                $options[] = $instance;
            }
            return $options;
        }
        return null;
    }

    public static function changeOption($name, $value, $type = "int") {
        $instance = new self();
        $instance->name = $name;
        $instance->userId = User::getCurrentUserId();
        $instance->value = $value;
        switch ($type) {
            case "int":
                $value = 0 + $value;
                break;
            case "array":
                $value = json_encode($value);
                break;
        }
        $db = Database::getInstance();
        $query = "SELECT * FROM user_options WHERE name = '$name' AND userid = $instance->userId";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow == 1) {
                $row = $result->dataTable->rows[0];
                $instance->id = $row['id'];
                $query = "UPDATE user_options SET value = '$value' WHERE name = '$name' AND userid = $instance->userId";
                $db->query($query);
            } else {
                $query = "INSERT INTO user_options (userid, name, value) VALUES ('$instance->userId', '$instance->name', '$value')";
                echo $query;
                $db->query($query);
            }
            return $instance;
        }
    }

}
